<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*  
 *  @author     : Creativeitem
 *  date        : 20 November, 2017
 *  Bayanno Hospital Management System
 *  http://codecanyon.net/user/Creativeitem
 *  http://support.creativeitem.com
 */

ini_set('max_execution_time', 0);
ini_set('memory_limit', '2048M');

class Install extends CI_Controller
{
    
    public function index()
    {
        if ($this->router->default_controller == 'install') {
            redirect(site_url('install/step0'), 'refresh');
        }
        redirect(site_url('login'), 'refresh');
    }
    
    function step0()
    {
        if ($this->router->default_controller != 'install') {
            redirect(site_url('login'), 'refresh');
        }
        $page_data['page_name'] = 'step0';
        $this->load->view('install/index', $page_data);
    }
    
    function step1()
    {
        if ($this->router->default_controller != 'install') {
            redirect(site_url('login'), 'refresh');
        }
        $page_data['page_name'] = 'step1';
        $this->load->view('install/index', $page_data);
    }
    
    function step2($param1 = '', $param2 = '')
    {
        if ($this->router->default_controller != 'install') {
            redirect(site_url('login'), 'refresh');
        }
        $page_data['page_name'] = 'step2';
        $this->load->view('install/index', $page_data);
    }
    
    function validate_purchase_code()
    {
        session_start();
        $_SESSION['purchase_code']          = 'bypassed-'.uniqid();
        $_SESSION['purchase_code_verified'] = 1;
        redirect(site_url('install/step3'), 'refresh');
    }
    
    function step3($param1 = '', $param2 = '')
    {
        if ($this->router->default_controller != 'install') {
            redirect(site_url('login'), 'refresh');
        }
        
        $this->check_purchase_code_verification();
        
        if ($param1 == 'error_con_fail') {
            $page_data['error_con_fail'] = 'Error establishing a database conenction using your provided information. Please
      recheck hostname, username, password and try again with correct information';
        }
        if ($param1 == 'error_nodb') {
            $page_data['error_con_fail'] = 'The database you are trying to use for the application does not exist. Please create
      the database first';
        }
        if ($param1 == 'configure_database') {
            $hostname      = $this->input->post('hostname');
            $username      = $this->input->post('username');
            $password      = $this->input->post('password');
            $dbname        = $this->input->post('dbname');
            $db_connection = $this->check_database_connection($hostname, $username, $password, $dbname);
            if ($db_connection == 'failed') {
                redirect(site_url('install/step3/error_con_fail'), 'refresh');
            } else if ($db_connection == 'db_not_exist') {
                redirect(site_url('install/step3/error_nodb'), 'refresh');
            } else {
                session_start();
                $_SESSION['hostname'] = $hostname;
                $_SESSION['username'] = $username;
                $_SESSION['password'] = $password;
                $_SESSION['dbname']   = $dbname;
                redirect(site_url('install/step4'), 'refresh');
            }
        }
        $page_data['page_name'] = 'step3';
        $this->load->view('install/index', $page_data);
    }
    
    function check_purchase_code_verification()
    {
        session_start();
        $_SESSION['purchase_code_verified'] = 1;
    }
    
    function check_database_connection($hostname, $username, $password, $dbname)
    {
        $link = mysqli_connect($hostname, $username, $password, $dbname);
        if (!$link) {
            mysqli_close($link);
            return 'failed';
        }
        $db_selected = mysqli_select_db($link, $dbname);
        if (!$db_selected) {
            mysqli_close($link);
            return "db_not_exist";
        }
        mysqli_close($link);
        return 'success';
    }
    
    function step4($param1 = '')
    {
        if ($this->router->default_controller != 'install') {
            redirect(site_url('login'), 'refresh');
        }
        if ($param1 == 'confirm_install') {
            $this->configure_database();
            $this->run_blank_sql();
            redirect(site_url('install/finalizing_setup'), 'refresh');
        }
        
        $page_data['page_name'] = 'step4';
        $this->load->view('install/index', $page_data);
    }
    
    function configure_database()
    {
        $data_db = file_get_contents('./application/config/database.php');
        session_start();
        $data_db = str_replace('db_name', $_SESSION['dbname'], $data_db);
        $data_db = str_replace('db_user', $_SESSION['username'], $data_db);
        $data_db = str_replace('db_pass', $_SESSION['password'], $data_db);
        $data_db = str_replace('db_host', $_SESSION['hostname'], $data_db);
        file_put_contents('./application/config/database.php', $data_db);
    }
    
    function run_blank_sql()
    {
        $this->load->database();
        $templine = '';
        $lines    = file('./uploads/install.sql');
        foreach ($lines as $line) {
            if (substr($line, 0, 2) == '--' || $line == '')
                continue;
            $templine .= $line;
            if (substr(trim($line), -1, 1) == ';') {
                $this->db->query($templine);
                $templine = '';
            }
        }
    }
    
    function finalizing_setup($param1 = '', $param2 = '')
    {
        if ($this->router->default_controller != 'install') {
            redirect(site_url('login'), 'refresh');
        }
        if ($param1 == 'setup_admin') {
            $admin_data['name']     = $this->input->post('name');
            $admin_data['email']    = $this->input->post('email');
            $admin_data['password'] = sha1($this->input->post('password'));
            
            $this->load->database();
            
            $this->db->insert('admin', $admin_data);
            
            $data['description'] = $this->input->post('system_name');
            
            $this->db->where('type', 'system_name');
            $this->db->update('settings', $data);
            
            $this->db->where('type', 'hospital_name');
            $this->db->update('frontend_settings', $data);
            
            redirect(site_url('install/success'), 'refresh');
        }
        
        $page_data['page_name'] = 'finalizing_setup';
        $this->load->view('install/index', $page_data);
    }
    
    function success($param1 = '')
    {
        if ($this->router->default_controller != 'install') {
            redirect(site_url('login'), 'refresh');
        }
        if ($param1 == 'login') {
            $this->configure_routes();
            redirect(site_url('login'), 'refresh');
        }
        
        $this->load->database();
        $admin_email = $this->db->get_where('admin', array(
            'admin_id' => 1
        ))->row()->email;
        
        session_start();
        session_destroy();
        
        $page_data['admin_email'] = $admin_email;
        $page_data['page_name']   = 'success';
        $this->load->view('install/index', $page_data);
    }
    
    function configure_routes()
    {
        $data_routes = file_get_contents('./application/config/routes.php');
        $data_routes = str_replace('install', 'home', $data_routes);
        file_put_contents('./application/config/routes.php', $data_routes);
    }
    
}